/******************************************
 *Author:庄育龙
 *Function:服务器设置对话框读取、存储设置的实现
 *Created Date:2013.05.22
 *Last Modified:2013.05.28
 ******************************************/

#include "netdialog.h"
#include "ui_netdialog.h"
#include <QSettings>
#include <QTextCodec>

NetDialog::NetDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NetDialog)
  //NetDialog的构造函数
{
    ui->setupUi(this);
    setfile = QApplication::applicationDirPath()+"/settings.ini";
    QSettings settings(setfile, QSettings::IniFormat);
    QByteArray nameBytes=settings.value("Name/ServerName").toByteArray();
    QTextCodec * codec=QTextCodec::codecForName("UTF8");
    QString name=codec->toUnicode(nameBytes);
    ui->lineEdit->setText(name);
    if(settings.value("First/Server").toInt()) {
        ui->radioButton->setChecked(1);
    }
    else {
        ui->radioButton_2->setChecked(1);
    }
    if(settings.value("Forbidden/NEnable").toInt()) {
        ui->checkBox->setChecked(1);
    }
    else {
        ui->checkBox->setChecked(0);
    }
    if(settings.value("Undo/Allow").toInt()) {
        ui->checkBox_2->setChecked(1);
    }
    else {
        ui->checkBox_2->setChecked(0);
    }
    //以上几行与Dialog的构造函数功能相似，从setttings.ini中读取设置并初始化

}

NetDialog::~NetDialog()
{
    delete ui;
}

void NetDialog::on_buttonBox_accepted()
//本函数在点击确定后把设置保存在设置文件中，供其他部分调用，也能保证在下一次开启程序时仍然是这个设置
{
    QSettings settings(setfile, QSettings::IniFormat);
    QString name=ui->lineEdit->text();
    if(ui->radioButton->isChecked()) {
        settings.setValue("First/Server",1);
    }
    else if(ui->radioButton_2->isChecked()) {
        settings.setValue("First/Server",0);
    }
    else{

    }

    if(ui->checkBox->isChecked()) {
        settings.setValue("Forbidden/NEnable",1);
    }
    else {
        settings.setValue("Forbidden/NEnable",0);
    }
    if(ui->checkBox_2->isChecked()) {
        settings.setValue("Undo/Allow",1);
    }
    else {
        settings.setValue("Undo/Allow",0);
    }
    settings.setValue("Name/ServerName",name.toUtf8().data());
}
